package cn.stylefeng.guns.rabbitmq.controller;

import cn.stylefeng.guns.rabbitmq.common.ProductData;
import cn.stylefeng.guns.rabbitmq.model.ResultParam;
import cn.stylefeng.guns.rabbitmq.service.ICardService;
import cn.stylefeng.guns.rabbitmq.util.DateStyle;
import cn.stylefeng.guns.rabbitmq.util.DateUtil;
import cn.stylefeng.guns.rabbitmq.util.HttpUtil;
import cn.stylefeng.guns.rabbitmq.util.ShouKaUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author jwj.
 * @Date 2019/8/421:40
 */
@RestController
@RequestMapping("/rabbitmq/card")
@Slf4j
public class SendApi {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Autowired
    Map<String, ICardService> cardChannelService;

    /**
     * 发送销卡的请求
     * @param jsonObject
     * @return
     */
    @PostMapping("/submit")
    public String submit(@RequestBody JSONObject jsonObject) {
        log.info("准备进入提交队列：" + jsonObject.toJSONString());
        rabbitTemplate.convertAndSend("pin_card_submit", jsonObject);
        return "ok";
    }

    /**
     * 批次提交的请求
     * @param jsonObject
     * @return
     */
    @PostMapping("/batch-submit")
    public String batchSubmit(@RequestBody JSONObject jsonObject) {
        log.info("准备进入批次提交队列：" + jsonObject.toJSONString());
        rabbitTemplate.convertAndSend("batch_pin_card_submit", jsonObject);
        return "ok";
    }


    /**
     * 接收销卡的回调请求
     * @param param
     * @return
     */
    @PostMapping("/notify-form/{service}")
    public Object notifyForm(@PathVariable("service") String serviceName, ResultParam param) {
        log.info("准备进入form回调队列：" + serviceName);
        param.setServiceName(serviceName);
        rabbitTemplate.convertAndSend("pin_card_notify", param);
        return cardChannelService.get(serviceName).returnOk();
    }

    /**
     * 接收销卡的回调请求
     * @param param
     * @return
     */
    @PostMapping("/notify-json/{service}")
    public Object notifyJSON(@PathVariable("service")  String serviceName, @RequestBody ResultParam param) {
        log.info("准备进入json回调队列：" + serviceName);
        param.setServiceName(serviceName);
        rabbitTemplate.convertAndSend("pin_card_notify", param);
        return cardChannelService.get(serviceName).returnOk();
    }

    /**
     * 初始化收卡啦的产品
     * @return
     */
    @GetMapping("/init-sk-product")
    public String initSkProduct(String operatorCode) {
        try {
            Map<String, String> resultMap = new HashMap<>();
            Map<String, String> param = new HashMap<>();
            param.put("userCode", "18771118457");
            param.put("operatorCode", operatorCode);
            param.put("dateTime", DateUtil.DateToString(new Date(), DateStyle.YYYYMMDDHHMMSS));
            //拼接参数顺序
            String paramMd5str = "userCodedateTime";
            for (String key : param.keySet()) {
                paramMd5str = paramMd5str.replace(key, param.get(key));
            }
            paramMd5str = paramMd5str.concat("a224d23317b22c1748e9659a3b449034");
            String sign = ShouKaUtil.MD5(paramMd5str);
            param.put("sign", sign);
            String resultStr = HttpUtil.sendPost("http://api.shoukala.com:8088/query/priceQuery ", param, "utf-8");
            log.info("获取收卡啦产品id：" + resultStr);
            JSONObject jsonObject = JSONObject.parseObject(resultStr);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            if (jsonArray != null) {
                for (Object pro : jsonArray) {
                    JSONObject object = (JSONObject) pro;
                    if (object.getString("state").equals("1")) {
                        if (object.get("api_code") != null) {
                            resultMap.put(object.getString("face_value"), object.getString("api_code"));
                        }
                    }
                }
            }
            ProductData.productMap.remove(operatorCode);
            if (resultMap.size() > 0) {
                ProductData.productMap.put(operatorCode, resultMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }
}
